package servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.io.XMLWriter;

import elements.Constants;
import elements.Deal;
import export.db.Loader;
import export.generator.Generator;

/**
 * Servlet implementation class DealsServlet
 */
public class DealsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Loader loader;
	private Generator generator;
	
	public DealsServlet() {
		super();
		loader = new Loader();
		generator = new Generator();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		String startTime = request.getParameter(Constants.PARAMETER_STARTTIME);
		String endTime = request.getParameter(Constants.PARAMETER_ENDTIME);
		String discountPercent = request.getParameter(Constants.PARAMETER_DISCOUNTPERCENT);
		String cityName = request.getParameter(Constants.PARAMETER_CITYNAME);
		if (cityName != null)
			cityName = new String(cityName.getBytes("ISO-8859-1"), "utf-8");
		String category = request.getParameter(Constants.PARAMETER_CATEGORY);
		if (category != null)
			category = new String(category.getBytes("ISO-8859-1"), "utf-8");
		String title = request.getParameter(Constants.PARAMETER_TITLE);
		if (title != null)
			title = new String(title.getBytes("ISO-8859-1"), "utf-8");
		String price = request.getParameter(Constants.PARAMETER_PRICE);
		String website = request.getParameter(Constants.PARAMETER_WEBSITE);
		String members = request.getParameter(Constants.PARAMETER_MEMBERS);
		
		String page = request.getParameter(Constants.PARAMETER_PAGE);
		String pageSize = request.getParameter(Constants.PARAMETER_PAGESIZE);
		int offset = 0;
		int length = -1;
		if (page != null && !page.isEmpty() && pageSize != null && !pageSize.isEmpty()) {
			length = Integer.parseInt(pageSize);
			offset = length * (Integer.parseInt(page) - 1);
		}
		String orderBy = request.getParameter(Constants.PARAMETER_ORDERBY);
		String order = request.getParameter(Constants.PARAMETER_ORDER);
		String oid = request.getParameter(Constants.PARAMETER_OID);
		String createTime = request.getParameter(Constants.PARAMETER_CREATETIME);
		List<Deal> deals = loader.loadDeals(createTime, oid, startTime, endTime, discountPercent, cityName, category, title, price, website, members, offset, length, orderBy, order);
		Document result = generator.getString(deals);
		
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		XMLWriter output = new XMLWriter(response.getWriter());
	    output.write( result );
		output.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
